package com.sgb.knowledgeshare.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.ActivityShareVideoinfoBinding;
import com.sgb.knowledgeshare.viewmodel.ShareUserVideoInfoModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.model.entity.VideoBean;
import com.swgk.core.dialog.RoundCornerDialog;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分享-用户中心-编辑视频-单个视频详情界面
 */
public class ShareUserVideoInfoActivity  extends AppActivity {

    ShareUserVideoInfoModel viewModel;
    private ActivityShareVideoinfoBinding binding;
    private VideoBean extra;
    private List<Long> delectList;

    public static void start(Context context, VideoBean bean) {
        Intent intent = new Intent();
        intent.putExtra("bean", (Serializable) bean);
        intent.setClass(context, ShareUserVideoInfoActivity.class);
        intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_share_videoinfo);
        viewModel = new ViewModelProvider(this).get(ShareUserVideoInfoModel.class);
        viewModel.setActivity(ShareUserVideoInfoActivity.this);
        View decor = getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        extra = (VideoBean) getIntent().getSerializableExtra("bean");
        binding.userVideoDelete.setOnClickListener(this);
        binding.userVideoCancel.setOnClickListener(this);
        binding.ivPlays.setVisibility(View.GONE);
        binding.video.setVisibility(View.VISIBLE);
        binding.video.setVideoPath(extra.getCoverURL());
        binding.video.start();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_plays) {//点击播放
            binding.ivPlays.setVisibility(View.GONE);
            playvideo();
        } else if (id == R.id.user_video_delete) {//删除
            delectList = new ArrayList<>();
            delectList.add((long) extra.getId());
            if (delectList.size() > 0) {
                videoDeleteDialog();
            }
        } else if (id == R.id.user_video_cancel) {//取消
            finish();
        }
    }

    private void playvideo() {
        binding.video.setVisibility(View.VISIBLE);
        binding.video.setVideoPath(extra.getCoverURL());
        binding.video.start();
        binding.video.setOnPreparedListener(mp -> {
            mp.setLooping(true);
            //延迟取消封面，避免加载视频黑屏
            new CountDownTimer(200, 200) {
                @Override
                public void onTick(long millisUntilFinished) {
                }

                @Override
                public void onFinish() {
                    binding.ivCover.setVisibility(View.GONE);
                }
            }.start();
        });
    }


    private void videoDeleteDialog() {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_message.setText("您的作品将被永久删除，无法找回。确认删除？");
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除视频的操作逻辑
                roundCornerDialog.dismiss();
                viewModel.deleteMyVideo(delectList);
            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
